<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="JADP-dialog JADP-dialog_center"
    lock-scroll
    width="1000px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
        :disabled="!!isDetail"
      >
        <el-col :span="24">
          <el-form-item label="名称" prop="name">
            <el-input
              v-model="dataForm.name"
              placeholder="请输入"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="地址" prop="address">
            <el-input
              v-model="dataForm.address"
              placeholder="请输入"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="描述" prop="describe">
            <el-input
              v-model="dataForm.describe"
              placeholder="请输入"
              :style="{ width: '100%' }"
              true
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 4 }"
            >
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="图片" prop="picture">
            <JADP-UploadImg
              v-model="pictureList"
              :fileSize="2"
              sizeUnit="MB"
              :limit="9"
            >
            </JADP-UploadImg>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="启用状态" prop="status">
            <el-switch v-model="dataForm.status" active-value="true">
            </el-switch>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="排序" prop="sorting">
            <el-input-number
              v-model="dataForm.sorting"
              placeholder="数字文本"
              :step="1"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import { previewDataInterface } from "@/api/systemData/dataInterface";
import ExportBox from "./ExportBox";
export default {
  components: { ExportBox },
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      activeName: "",
      dataForm: {
        name: undefined,
        address: undefined,
        describe: undefined,
        picture: [],
        status: 1,
        sorting: undefined,
        creatoruserid: "",
        creatortime: "",
        lastmodifyuserid: "",
        lastmodifytime: "",
        organizeId:""
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        picture: [{ required: true, message: "请上传图片", trigger: "blur" }]
      },
      pictureList: [],
      picture: []
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    init(id, isDetail) {
      this.pictureList = [];
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/banner/Xy_ks_banner/" + this.dataForm.id,
            method: "get"
          }).then(res => {
            this.dataForm = res.data;
            this.pictureList = this.dataForm.picture
              ? JSON.parse(this.dataForm.picture)
              : [];
            this.loading = false;
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$set(
        this.dataForm,
        "picture",
        this.pictureList.length ? JSON.stringify(this.pictureList) : ""
      );
      this.$refs["elForm"].validate(valid => {
        if (valid) {
          this.dataForm.organizeId= this.$store.getters.organizeId;
          if (!this.dataForm.id) {
            request({
              url: `/api/banner/Xy_ks_banner`,
              method: "post",
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  (this.visible = false), this.$emit("refresh", true);
                }
              });
            });
          } else {
            request({
              url: "/api/banner/Xy_ks_banner/" + this.dataForm.id,
              method: "PUT",
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refresh", true);
                }
              });
            });
          }
        }
      });
    }
  }
};
</script>
